//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "VolatilityTermStructure.h"
using namespace Cephei::QL::Termstructures;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/TermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::CVolatilityTermStructure::CVolatilityTermStructure (boost::shared_ptr<QuantLib::VolatilityTermStructure>& childNative, Object^ owner) : CTermStructure(CVolatilityTermStructure::typeid)
{
#ifdef HANDLE
	_phVolatilityTermStructure = NULL;
#endif
	_ppVolatilityTermStructure = &childNative;
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppVolatilityTermStructure));
}
Cephei::QL::Termstructures::CVolatilityTermStructure::CVolatilityTermStructure (QuantLib::VolatilityTermStructure& childNative, Object^ owner) : CTermStructure(CVolatilityTermStructure::typeid)
{
#ifdef HANDLE
	_phVolatilityTermStructure = NULL;
#endif
	_ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (&childNative);
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppVolatilityTermStructure));
    _VolatilityTermStructureOwner = owner;
    _TermStructureOwner = owner;
}

Cephei::QL::Termstructures::CVolatilityTermStructure::CVolatilityTermStructure (CVolatilityTermStructure^ copy) : CTermStructure(CVolatilityTermStructure::typeid)
{
#ifdef HANDLE
	_phVolatilityTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (copy->GetShared());
        _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppVolatilityTermStructure));
    }
}
Cephei::QL::Termstructures::CVolatilityTermStructure::CVolatilityTermStructure (PLATFORM::Type^ t) : CTermStructure(CVolatilityTermStructure::typeid)
{
#ifdef HANDLE
	_phVolatilityTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CVolatilityTermStructure::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::CVolatilityTermStructure::CVolatilityTermStructure (QuantLib::Handle<QuantLib::VolatilityTermStructure>& childNative, Object^ owner)  : CTermStructure(CVolatilityTermStructure::typeid)
{
	_phVolatilityTermStructure = &childNative;
	_ppVolatilityTermStructure = &static_cast<boost::shared_ptr<QuantLib::VolatilityTermStructure>>(childNative.currentLink());
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppVolatilityTermStructure));
    _VolatilityTermStructureOwner = owner;
}
Cephei::QL::Termstructures::CVolatilityTermStructure::CVolatilityTermStructure (QuantLib::Handle<QuantLib::VolatilityTermStructure> childNative)  : CTermStructure(CVolatilityTermStructure::typeid)
{
	_phVolatilityTermStructure = &childNative;
	_ppVolatilityTermStructure = &static_cast<boost::shared_ptr<QuantLib::VolatilityTermStructure>>(childNative.currentLink());
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppVolatilityTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::CVolatilityTermStructure::CVolatilityTermStructure (QuantLib::VolatilityTermStructure childNative)  : CTermStructure(CVolatilityTermStructure::typeid)
{
#ifdef HANDLE
	_phVolatilityTermStructure = NULL;
#endif
	_ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (new QuantLib::VolatilityTermStructure (childNative));
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppVolatilityTermStructure));
}
#endif

Cephei::QL::Termstructures::CVolatilityTermStructure::~CVolatilityTermStructure ()
{
    if (_ppVolatilityTermStructure != NULL)
    {
	    delete _ppVolatilityTermStructure;
        _ppVolatilityTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::CVolatilityTermStructure::!CVolatilityTermStructure ()
{
    if (_ppVolatilityTermStructure != NULL)
    {
	    delete _ppVolatilityTermStructure;
    }
}
QuantLib::VolatilityTermStructure& Cephei::QL::Termstructures::CVolatilityTermStructure::GetReference ()
{
    if (_ppVolatilityTermStructure == NULL) throw REFNEW NativeNullException ();
	return **_ppVolatilityTermStructure;
}
boost::shared_ptr<QuantLib::VolatilityTermStructure>& Cephei::QL::Termstructures::CVolatilityTermStructure::GetShared ()
{
    if (_ppVolatilityTermStructure == NULL) throw REFNEW NativeNullException ();
	return *_ppVolatilityTermStructure;
}
QuantLib::VolatilityTermStructure* Cephei::QL::Termstructures::CVolatilityTermStructure::GetPointer ()
{
    if (_ppVolatilityTermStructure == NULL) throw REFNEW NativeNullException ();
	return &**_ppVolatilityTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VolatilityTermStructure>& Cephei::QL::Termstructures::CVolatilityTermStructure::GetHandle ()
{
	if (_phVolatilityTermStructure == NULL)
	{
		_phVolatilityTermStructure = new Handle<QuantLib::VolatilityTermStructure> (*_ppVolatilityTermStructure);
	}
	return *_phVolatilityTermStructure;
}
#endif
bool Cephei::QL::Termstructures::CVolatilityTermStructure::HasNative () 
{
	return (_ppVolatilityTermStructure != NULL);
}

QL::Times::BusinessDayConventionEnum Cephei::QL::Termstructures::CVolatilityTermStructure::BusinessDayConvention::get ()
{
    try
    {
    	QuantLib::BusinessDayConvention _rv = (QuantLib::BusinessDayConvention)(*_ppVolatilityTermStructure)->businessDayConvention ( );   
        QL::Times::BusinessDayConventionEnum _nrv = (QL::Times::BusinessDayConventionEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::CVolatilityTermStructure::MaxStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppVolatilityTermStructure)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::CVolatilityTermStructure::MinStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppVolatilityTermStructure)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::CVolatilityTermStructure::OptionDateFromTenor (Cephei::QL::Times::IPeriod^ prm1)
{
    CPeriod^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CPeriod^> (prm1);
        _Cprm1->Lock();
        QuantLib::Period& _prm1 = static_cast<QuantLib::Period&> (_Cprm1->GetReference ()); 
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppVolatilityTermStructure)->optionDateFromTenor ( _prm1 );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

